package com.yswl.pri.home.vedio;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yswl.pri.R;
import com.yswl.pri.common.ToolbarActivity;
import com.yswl.pri.picture.SpacesItemDecoration;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import yswl.com.klibrary.base.BaseRecyclerAdapter;
import yswl.com.klibrary.base.BaseViewHolder;
import yswl.com.klibrary.util.L;
import yswl.com.klibrary.util.WaitingDialogUtil;

/**
 * 搜索的电视剧集数
 */
public class MoviePageListActivity extends ToolbarActivity {
    private static final String TAG = MoviePageListActivity.class.getSimpleName();
    MovieListAdapter movieListAdapter;
    Dialog dialog;

    public static void JumpAct(Context context, String url, String title) {
        Intent intent = new Intent(context, MoviePageListActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_list);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        title = getIntent().getStringExtra("title");
        setToolbarTitle(title);
        initUI();
        String url = getIntent().getStringExtra("url");
        L.d(TAG,"final url:"+url);
        new MyAsyncTask().execute(url);
    }

    private void initUI() {
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.requestFocus();
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        SpacesItemDecoration decoration = new SpacesItemDecoration(5);
        recyclerView.addItemDecoration(decoration);
        movieListAdapter = new MovieListAdapter(this, null, R.layout.activity_movie_list_item);
        recyclerView.setAdapter(movieListAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    class MyAsyncTask extends AsyncTask<String, Void, List<Program>> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = WaitingDialogUtil.getWaitDialog(MoviePageListActivity.this);
            dialog.show();
        }

        @Override
        protected List<Program> doInBackground(String... strings) {
            String url = strings[0];
            try {
                Document document = Jsoup.connect(url).get();
                Element e = document.body();
                if (e == null) return null;
                Elements vediobannel = e.select(".stui-content__playlist");
                Elements vedioUrlEleList = vediobannel.select("li > a");
                if (vedioUrlEleList == null || vedioUrlEleList.size() == 0) return null;

                List<Program> count = new ArrayList<>();
                for (Element element : vedioUrlEleList) {
                    Program program = new Program();
                    L.e(TAG, " element : " + element.toString());
                    program.url = element.attr("href");
                    program.name = element.attr("title");
//                    program.name = element.text();
                    count.add(program);
                }
                return count;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<Program> lists) {
            super.onPostExecute(lists);
            dialog.cancel();
            if (lists != null && lists.size() > 0) {
                movieListAdapter.setList(lists);
            }
        }
    }

    public class MovieListAdapter extends BaseRecyclerAdapter<Program> {


        public MovieListAdapter(Context mContext, List<Program> list, int itemLayoutId) {
            super(mContext, list, itemLayoutId);
        }

        @Override
        public void onBind(BaseViewHolder holder, final Program data, int position) {
            final TextView textView = holder.findById(R.id.name);
            textView.setText(data.name);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    textView.setTextColor(getResources().getColor(R.color.colorAccent));
                    BrowserActivity.JumpAct(v.getContext(), data.name, data.url);
                }
            });
        }
    }
}
